#ifndef   GAME_KEYPAD_HY
#define   GAME_KEYPAD_HY


//#include <winuser.h>

#define    KEYDOWN(vk_code)    (GetAsyncKeyState(vk_code)&0x8000 ? 1:0 )

#define KEY_NUM0       0x60
#define KEY_NUM1       0x61
#define KEY_NUM2       0x62
#define KEY_NUM3       0x63
#define KEY_NUM4       0x64
#define KEY_NUM5       0x65
#define KEY_NUM6       0x66
#define KEY_NUM7       0x67
#define KEY_NUM8       0x68
#define KEY_NUM9       0x69

#define NOK_UP_ARROW      0x26
#define NOK_DOWN_ARROW    0x28
#define NOK_LEFT_ARROW    0x25
#define NOK_RIGHT_ARROW   0x27

#define NOK_MIDDLE        0x6D

#define KEY_POUND         0x24 //0x6F
#define	KEY_STAR          0x21 //0x6A
#define	NOK_LEFT_SOFT     0x23
#define	NOK_RIGHT_SOFT    0x22


//// nokia phone key code
//#define NOK_UP_ARROW      (-1)
//#define NOK_DOWN_ARROW    (-2)
//#define NOK_LEFT_ARROW    (-3)
//#define NOK_RIGHT_ARROW   (-4)
//#define NOK_MIDDLE        (-5) 
//#define NOK_LEFT_SOFT     (-6)
//#define NOK_RIGHT_SOFT    (-7)
//
//
//// motorola phone key code
//#define MOTO_UP_ARROW     (-1)
//#define MOTO_DOWN_ARROW   (-6)
//#define MOTO_LEFT_ARROW   (-2)
//#define MOTO_RIGHT_ARROW  (-5)
//#define MOTO_MIDDLE       (-20)
//#define MOTO_LEFT_SOFT    (-21)
//#define MOTO_RIGHT_SOFT   (-22)

//=============================================
const int GK_NUM2         = 1;
const int GK_NUM3         = 1 << 1;
const int GK_NUM6         = 1 << 2;
const int GK_NUM9         = 1 << 3;
const int GK_NUM8         = 1 << 4;
const int GK_NUM7         = 1 << 5;
const int GK_NUM4         = 1 << 6;
const int GK_NUM1         = 1 << 7;
const int GK_NUM5         = 1 << 8;
const int GK_POUND        = 1 << 9;
const int GK_NUM0         = 1 << 10;
const int GK_STAR         = 1 << 11;
const int GK_LEFT_SOFT    = 1 << 12;
const int GK_RIGHT_SOFT   = 1 << 13;
const int GK_PAD_UP       = 1 << 14;
const int GK_PAD_RIGHT    = 1 << 15;
const int GK_PAD_DOWN     = 1 << 16;
const int GK_PAD_LEFT     = 1 << 17;     
const int GAME_KEY_COUNT  = 18;

const int GK_UPLEFT = GK_NUM1;
const int GK_UP = GK_NUM2 | GK_PAD_UP;
const int GK_UPRIGHT = GK_NUM3;
const int GK_LEFT = GK_NUM4 | GK_PAD_LEFT; //GK_RIGHT;
const int GK_RIGHT = GK_NUM6 | GK_PAD_RIGHT; //GK_LEFT;
const int GK_DOWNLEFT = GK_NUM7;
const int GK_DOWN = GK_NUM8 | GK_PAD_DOWN;
const int GK_DOWNRIGHT = GK_NUM9;
const int GK_NUM_KEY = GK_NUM1 | GK_NUM2 | GK_NUM3 | GK_NUM4 | GK_NUM5 |
          GK_NUM6 | GK_NUM7 | GK_NUM8 | GK_NUM9;
const int GK_OK = GK_NUM5 | GK_LEFT_SOFT;
const int GK_CANCEL = GK_RIGHT_SOFT;
//===============================================
// standard phone key code
const int STD_EXIT = -11;

const int CONTROL_90_DEGREE = -1;
const int CONTROL_45_DEGREE = 1;

const int DOUBLEPRESS_INTERVAL = 5;
const int DOUBLEPRESS_PASS_INTERVAL = 5;//feedback, hard to do one-two
const int DOUBLEPRESS_FEINT_INTERVAL = 4;
const int KEYHOLD_FEINT_INTERVAL = 3;
const int KEYHOLD_MOVE90_INTERVAL = 4;

const int GAMEKEY_QUEUE_SIZE = 6;

class CKeyPad
{
private:
	long m_lastPress[GAME_KEY_COUNT];
	long m_intervalPress[GAME_KEY_COUNT];
	unsigned int m_keyCurrent;
	unsigned int m_keyPressed;
	unsigned int m_keyReleased;
	unsigned int m_keysDisabled;


	int m_keyTick;
	unsigned int m_lastPressed;
	unsigned int m_keyDblPressed;
	unsigned int m_fastCurrentKey;
	int m_gkQueue[GAMEKEY_QUEUE_SIZE];
	long m_gkTimeQueue[GAMEKEY_QUEUE_SIZE];//store the press key time
	unsigned int m_queueStart;
	unsigned int m_queueEnd;

	long m_tickCounter;

public:
	CKeyPad();
	~CKeyPad();
	void InitKey();
	void ClearKey();
	void DisableKey(int frames);
	bool PushQueue(unsigned int key);
	int PopQueue();
	void UpdateKey();
	bool IsKeyHold(unsigned int gameKey);
	bool IsAnyKeyPressed();
	bool IsKeyDoublePressed(unsigned int gameKey);
	bool IsKeyPressed(unsigned int keyCode);
	bool IsKeyReleased(unsigned int keyCode);
	void KeyPressed(unsigned int keyCode);
	void KeyReleased(unsigned int keyCode);
	bool IsAnyKeyHold();
	long GetKeyInterval(unsigned int gameKey);
	int GetKey(unsigned int keyCode);
};

#endif